package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.utils.MD5Utils;
import com.mzxy.utils.SpUtiles;

public class Loginctivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etUserName;
    private EditText etPwd;
    private Button btnLogin;
    private TextView tvRegister;
    private TextView tvFindPsw;
    private TextView tvBack;
    private TextView tvMainTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginctivity);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        tvMainTitle.setText("登录");
        tvBack = (TextView) findViewById(R.id.tv_back);
        etUserName = (EditText) findViewById(R.id.et_user_name);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnLogin = (Button) findViewById(R.id.btn_login);
        tvRegister = (TextView) findViewById(R.id.tv_register);
        tvFindPsw = (TextView) findViewById(R.id.tv_find_psw);
        initView();
        initEvent();

    }

    private void initEvent() {
        btnLogin.setOnClickListener(this);
        tvBack.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvFindPsw.setOnClickListener(this);

    }


    private void initView() {
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                Login();
                break;
            case R.id.tv_register:
                startActivityForResult(new Intent(this,RegisterActivity.class) ,0x001);


                break;
            case R.id.tv_find_psw:
                break;

        }

    }
    protected void onActivityResult(int requestCode,int result,Intent date){
        String username = date.getStringExtra("username");
        String password = date.getStringExtra("password");
        etUserName.setText(username);
        etPwd.setText(password);


    }

    private void Login() {
        //先判断不为空
       /* SharedPreferences sp = getSharedPreferences(Constants.SP_NAME,MODE_PRIVATE);*/
        String username = etUserName.getText().toString();
        String password = etPwd.getText().toString();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
            Toast.makeText(this,"账号和密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        //如果根据用户名能找到密码，贼说明用户名已经注册
        String oldPwdMD5 = SpUtiles.getPsw(this,username);
        if(TextUtils.isEmpty(oldPwdMD5)){
            Toast.makeText(this,"该用户名未注册",Toast.LENGTH_SHORT).show();
            return;
        }
        String pwdmd5 = MD5Utils.md5(password);
        if (oldPwdMD5.equals(pwdmd5)){
            Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
            finish();
        }
        else {
            Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show();
        }
    }
}
